<template>
  <div class="flexContent">
    <div class="searchBox">
      <el-form ref="formRef" class="demo-form-inline" @submit.native.prevent label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="使用门店">
                  <el-input v-model="storeName" style="width: 95%" placeholder="请输入..." clearable
                    @keyup.enter.native="serchFn" />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="设备编号">
                  <el-input v-model="deviceNum" style="width: 95%" placeholder="请输入..." clearable
                    @keyup.enter.native="serchFn" />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="设备ID">
                  <el-input v-model="deviceId" style="width: 95%" placeholder="请输入..." clearable
                    @keyup.enter.native="serchFn" />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="设备类型">
                  <el-select v-model="deviceType" filterable clearable popper-class="eloption" style="width: 95%">
                    <el-option v-for="(val, key) in typeList" :key="key" :value="key" :label="val" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="在线状态">
                  <el-select v-model="isOnline" clearable popper-class="eloption" style="width: 95%">
                    <el-option value="1" label="在线" />
                    <el-option value="0" label="离线" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="serchFn" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="resetFn" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="add">
      <el-button class="addBtn" @click="addFn">新增</el-button>
      <el-button class="addBtn" @click="handoverFn">
        异常交班
      </el-button>
    </div>
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" class="tablebox" border ref="xTable1"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" style="width: 100%" height="100%"
          align="center">
          <vxe-column type="seq" title="序号" width="80"></vxe-column>
          <vxe-column field="storeName" title="使用门店" />
          <vxe-column field="deviceType" title="设备类型">
            <template v-slot="{ row }">
              <span>{{ row.deviceType | filterType(typeList) }}</span>
            </template>
          </vxe-column>
          <vxe-column field="deviceNum" title="设备编号" />
          <vxe-column field="deviceId" title="设备ID" />
          <vxe-column title="设备状态">
            <template v-slot="{ row }">
              <el-tag v-if="row.status === '0'" disabled type="primary" plain size="small">
                已启用
              </el-tag>
              <el-tag v-if="row.status === '1'" disabled type="info" plain size="small">
                已停用
              </el-tag>
              <el-tag v-if="row.status === '2'" disabled type="danger" plain size="small">
                故 障
              </el-tag>
            </template>
          </vxe-column>
          <vxe-column title="在线状态">
            <template v-slot="{ row }">
              <el-tag v-if="row.isOnline === '1'" disabled type="primary" plain size="small">
                在线
              </el-tag>
              <el-tag v-if="row.isOnline === '0'" disabled type="info" plain size="small">
                离线
              </el-tag>
            </template>
          </vxe-column>
          <vxe-column title="操作" fixed="right" width="150">
            <template v-slot="{ row }">
              <el-button type="text" size="big" style="margin-right: 10px; color: rgb(0, 132, 255)"
                @click.stop="getDetail(row)">
                详情
              </el-button>
              <el-button slot="reference" style="color: rgb(0, 132, 255)" type="text" size="big" @click.stop="edit(row)">
                修改
              </el-button>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,->, sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
    <AddDialog :close-on-click-modal="false" :show-dialog.sync="showDialog" :width="width" :title="title" @btnOk="btnOk"
      @close="close">
      <el-form ref="addFormRef" :disabled="title === '详情'" :model="addForm" :rules="addRules" label-width="130px">
        <el-row class="btnt" :gutter="24">
          <el-col :span="12">
            <el-form-item label="设备编号" style="width: 90%" prop="deviceNum">
              <el-input v-model="addForm.deviceNum" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="厂商" style="width: 90%" prop="manufacturer">
              <el-select v-model="addForm.manufacturer" filterable clearable popper-class="eloption" style="width: 100%">
                <el-option v-for="(val, key) in posmanufacturerList" :key="key" :value="key" :label="val" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="btnt" :gutter="24">
          <el-col :span="12">
            <el-form-item label="设备ID" style="width: 90%" prop="deviceId">
              <el-input v-model="addForm.deviceId" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备类型" style="width: 90%" prop="deviceType">
              <el-select v-model="addForm.deviceType" filterable clearable popper-class="eloption" style="width: 100%">
                <el-option v-for="(val, key) in typeList" :key="key" :value="key" :label="val" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row class="btnt" :gutter="24">
          <el-col :span="12">
            <el-form-item label="购买日期" prop="purchaseDate" style="width: 90%">
              <el-date-picker v-model="addForm.purchaseDate" style="width: 100%" format="yyyy-MM-dd"
                value-format="yyyy-MM-dd" type="date" placeholder="选择日期" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备状态" style="width: 90%" prop="status">
              <el-radio-group v-model="addForm.status" size="small">
                <el-radio label="0">启用</el-radio>
                <el-radio label="1">停用</el-radio>
                <el-radio label="2">故障</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="btnt" :gutter="24">
          <el-col :span="12">
            <el-form-item label="质保到期日期" prop="expireDate" style="width: 90%">
              <el-date-picker v-model="addForm.expireDate" style="width: 100%" format="yyyy-MM-dd"
                value-format="yyyy-MM-dd" type="date" placeholder="选择日期" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="质保期(月)" style="width: 90%" prop="warrantyPeriod">
              <el-input v-model="addForm.warrantyPeriod" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="btnt" :gutter="24">
          <el-col :span="12">
            <el-form-item label="使用门店" style="width: 90%" prop="storeId">
              <el-select v-model="addForm.storeId" filterable clearable popper-class="eloption" style="width: 100%">
                <el-option v-for="shop in shopList" :key="shop.id" :value="shop.id" :label="shop.name" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备品牌" style="width: 90%" prop="deviceBrand">
              <el-input v-model="addForm.deviceBrand" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="btnt" :gutter="24" v-if="addForm.storeId">
          <el-col :span="12">
            <el-form-item label="门店使用日期" prop="storePurchaseDate" style="width: 90%">
              <el-date-picker v-model="addForm.storePurchaseDate" style="width: 100%" format="yyyy-MM-dd"
                value-format="yyyy-MM-dd" type="date" placeholder="选择日期" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="门店质保到期日期" prop="storeExpireDate" style="width: 90%">
              <el-date-picker v-model="addForm.storeExpireDate" style="width: 100%" format="yyyy-MM-dd"
                value-format="yyyy-MM-dd" type="date" placeholder="选择日期" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" class="btnt">
          <el-col :span="21">
            <el-form-item label="设备备注" style="width: 100%">
              <el-input v-model="addForm.remark" type="textarea" maxlength="100" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </AddDialog>
    <AddDialog :close-on-click-modal="false" :show-dialog.sync="showDialog2" width="30%" title="异常交班" @btnOk="btnOk2"
      @close="close2">
      <el-form ref="addFormRef2" :model="addForm2" label-width="130px" :rules="addRules2">
        <el-row :gutter="24" class="btnt">
          <el-col :span="24">
            <el-form-item label="设备号" style="width: 90%" prop="deviceId">
              <el-input v-model.trim="addForm2.deviceId" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" class="btnt">
          <el-col :span="24">
            <el-form-item label="用户名" style="width: 90%" prop="userName">
              <el-input v-model.trim="addForm2.userName" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </AddDialog>
  </div>
</template>

<script>
import AddDialog from '@/components/Goods/AddDialog';
import {
  getPosList,
  getPosDetail,
  getShopEasy,
  addPos,
  updatePosDetail,
  exceptionShift
} from '@/api/shop';
import { getDictValueAndNameSource } from '@/api/dictionary';
import { parseTime3 } from '@/utils';
export default {
  name: 'GoodsInfo',
  components: {
    AddDialog
  },
  filters: {
    filterType(val, typeList) {
      for (let key in typeList) {
        if (key == val) return typeList[key]
      }
    }
  },
  data() {
    return {
      width: '50%',
      title: '新增',
      showDialog: false,
      showDialog2: false,
      addForm: {
        deviceNum: '', // 设备编号
        storeId: '', // 使用门店id
        deviceBrand: '', // 设备品牌
        deviceId: '', //	设备id
        purchaseDate: null, // 购买日期
        status: '0', // 设备状态:0.启用，1.停用
        manufacturer: '', //厂商
        deviceType: '', // 设备类型
        expireDate: '', // 质保到期日期
        warrantyPeriod: '', // 质保期
        storePurchaseDate: '', // 门店使用日期
        storeExpireDate: '', // 门店质保到期日期
        remark: '' // 设备备注
      },
      addRules: {
        manufacturer: [
          {
            required: true,
            message: '请选择厂商',
            trigger: 'blur'
          }
        ],
        deviceNum: [
          {
            required: true,
            message: '请输入设备编号',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 20,
            message: '长度在1到20个字符',
            trigger: 'blur'
          },
          {
            pattern: /^[0-9a-zA-Z]+$/,
            message: '只能输入数字和字母',
            trigger: 'blur'
          }
        ],
        purchaseDate: [
          {
            required: true,
            message: '请选择购买日期',
            trigger: 'blur'
          }
        ],
        expireDate: [
          {
            required: true,
            message: '请选择质保到期日期',
            trigger: 'blur'
          }
        ],
        storePurchaseDate: [
          {
            required: true,
            message: '请选择门店使用日期',
            trigger: 'blur'
          }
        ],
        storeExpireDate: [
          {
            required: true,
            message: '请选择门店质保到期日期',
            trigger: 'blur'
          }
        ],
        warrantyPeriod: [
          {
            required: true,
            message: '请输入质保期',
            trigger: 'blur'
          },
          {
            pattern: /^\+?[0-9]\d*$/,
            message: '请输入数字',
            trigger: 'blur'
          }
        ],
        deviceType: [
          {
            required: true,
            message: '请选择设备类型',
            trigger: 'blur'
          }
        ],
        deviceBrand: [
          {
            min: 1,
            max: 20,
            message: '长度在1到20个字符',
            trigger: 'blur'
          }
        ],
        deviceId: [
          {
            required: true,
            message: '请输入设备ID',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 30,
            message: '长度在1到30个字符',
            trigger: 'blur'
          },
          {
            pattern: /^[0-9a-zA-Z]+$/,
            message: '只能输入数字和字母',
            trigger: 'blur'
          }
        ],
        status: [
          {
            required: true,
            message: '请选择设备状态',
            trigger: 'blur'
          }
        ]
      },
      isSearch: false,
      shopList: [],
      isOnline: '',
      storeName: '',
      deviceId: '',
      deviceType: '',
      deviceNum: '',
      posmanufacturerList: {},
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0
      },
      posList: [],
      typeList: [],
      posId: '',
      addForm2: {
        deviceId: '',
        userName: ''
      },
      addRules2: {
        deviceId: [
          {
            required: true,
            message: '请输入设备ID',
            trigger: 'blur'
          },
        ],
        userName: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          },
        ]
      }
    };
  },
  created() {
    this.getPosList();
    this.getShopEasy();
    this.getPosmanufacturerList();
    this.getPosType();
  },
  methods: {
    async getPosmanufacturerList() {
      const res = await getDictValueAndNameSource('POS_MANUFACTURER');
      if (res.code === 200) {
        this.posmanufacturerList = res.data;
      }
    },
    async getPosType() {
      const res = await getDictValueAndNameSource('POS_TYPE');
      if (res.code === 200) {
        this.typeList = res.data;
        console.log('this.typeList', this.typeList);
      }
    },
    async getShopEasy() {
      const res = await getShopEasy();
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        console.log('shopList', result);
        this.shopList = result.store;
        console.log('shopList', this.shopList);
      } else {
        // this.$message.error(res.message);
      }
    },
    async getPosList() {
      if (!this.isSearch) {
        this.deviceId = '';
        this.storeName = '';
        this.deviceNum = '';
        this.deviceType = ''
        this.isOnline = ''
      }
      const res = await getPosList({
        ...this.page,
        name: this.storeName,
        deviceId: this.deviceId,
        deviceNum: this.deviceNum,
        isOnline: this.isOnline,
        deviceType: this.deviceType
      });
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        this.posList = result.list;
        this.page.pageSize = result.pageSize;
        this.page.pageNum = result.pageNum;
        this.page.total = result.total;
        console.log('posList', this.posList);
        const $table = this.$refs.xTable1
        $table.loadData(this.posList);
      } else {
        // this.$message.error(res.message);
      }
    },
    async serchFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0
      };
      this.isSearch = true;
      this.getPosList();
    },
    resetFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0
      };
      this.deviceId = '';
      this.storeName = '';
      this.deviceNum = '';
      this.deviceType = ''
      this.isOnline = ''
      this.isSearch = false;
      this.getPosList();
    },
    addFn() {
      this.title = '新增';
      this.showDialog = true;
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      console.log(`每页 ${val} 条`);
      this.getPosList();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page.pageNum = val;
      this.getPosList();
    },
    close() {
      this.showDialog = false;
      this.addForm = {
        deviceNum: '', // 设备编号
        storeId: '', // 使用门店id
        deviceBrand: '', // 设备品牌
        deviceId: '', //	设备id
        purchaseDate: '', // 购买日期
        status: '0', // 设备状态:0.启用，1.停用
        remark: ''
      };
      this.$refs.addFormRef.resetFields();
    },
    btnOk() {
      // 判断是新增还是修改
      if (this.title === '新增') {
        // 新增
        this.$refs.addFormRef.validate(async isOk => {
          if (isOk) {
            //
            if (!this.addForm.storeId) {
              this.addForm.storePurchaseDate = null;
              this.addForm.storeExpireDate = null;
            }
            console.log('this.addForm', this.addForm);
            // return
            const res = await addPos(this.addForm);
            if (res.code === 200) {
              this.$message.success('新增成功');
              this.getPosList();
              this.close();
            } else {
              // this.$message.error(res.message);
            }
          } else {
            this.$message.warning('请检查输入信息');
          }
        });
      } else if (this.title === '详情') {
        // 关闭
        this.close();
      } else if (this.title === '修改') {
        // 修改
        this.$refs.addFormRef.validate(async isOk => {
          if (isOk) {
            if (!this.addForm.storeId) {
              this.addForm.storePurchaseDate = null;
              this.addForm.storeExpireDate = null;
            }
            const res = await updatePosDetail({
              ...this.addForm,
              id: this.posId
            });
            if (res.code === 200) {
              this.$message.success('修改成功');
              this.getPosList();
              this.close();
            } else {
              // this.$message.error(res.message);
            }
          } else {
            this.$message.warning('请检查输入信息');
          }
        });
      }
    },
    async getDetail(row) {
      this.showDialog = true;
      this.title = '详情';
      console.log(row);
      const res = await getPosDetail({ id: row.id });
      if (res.code === 200) {
        const data = res.data;
        this.addForm = data;
        console.log('addForm', this.addForm);
        console.log(data?.purchaseDate, ' data?.purchaseDate ');
        if (!data.purchaseDate) {
          console.log(11111111);
          this.addForm.purchaseDate = '';
        }

        this.addForm.purchaseDate = data?.purchaseDate
          ? parseTime3(data.purchaseDate)
          : '';

        console.log(this.addForm.purchaseDate, 'time');
      } else {
        // this.$message.error(res.message);
      }
    },
    async edit(row) {
      this.showDialog = true;
      this.title = '修改';
      console.log(row);
      this.posId = row.id;
      const res = await getPosDetail({ id: row.id });
      if (res.code === 200) {
        const data = res.data;
        console.log(data, 'data');
        this.addForm = data;
        this.$set(
          this.addForm,
          'warrantyPeriod',
          String(
            res.data.warrantyPeriod ? res.data.warrantyPeriod : 0
          )
        );
        // console.log('addForm', this.addForm)
        if (!data.purchaseDate) {
          console.log(11111111);
          this.addForm.purchaseDate = '';
        } else {
          this.addForm.purchaseDate = parseTime3(data.purchaseDate);
        }
      } else {
        // this.$message.error(res.message);
      }
    },
    handoverFn() {
      this.showDialog2 = true;
    },
    // 异常交班
    btnOk2() {
      this.$refs.addFormRef2.validate(async isOk => {
        if (isOk) {
          //
          console.log('this.addForm2', this.addForm2);
          const res = await exceptionShift(this.addForm2);
          if (res.code === 200) {
            this.$message.success('新增成功');
            this.addForm2 = {
              deviceId: '',
              userName: ''
            }
            this.showDialog2 = false;
          } else {
            // this.$message.error(res.message);
          }
        }
      });
    },
    close2() {
      this.showDialog2 = false;
      // 清空数据
      this.addForm2 = {
        deviceId: '',
        userName: ''
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.shopInfoContent {
  padding: 10px;

  /* // background-color: #eceaea; */
  .searchBox {
    padding-top: 20px;
    /* // height: 20%; */
    background-color: #fff;
    margin-bottom: 10px;
  }

  .table {
    padding: 20px;
    background: #fff;
  }
}

.btnt {
  margin-bottom: 15px !important;
}
</style>
